﻿using System;
using System.Web.UI.WebControls;
using Wei8808Cms.BLL;
using Wei8808Cms.Model;
using Wei8808Cms.WebSite.UI;

namespace Wei8808Cms.WebSite.admin.News
{
    public partial class EditNewsInfoForm : BasicPage
    {
        public int Id;

        private readonly NewsBLL _bll = new NewsBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!int.TryParse(Request.Params["id"] as string, out this.Id))
            {
                JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改的信息不存在或参数不正确。", "back", "Error");
                return;
            }
            if (!Page.IsPostBack)
            {
                BindDropdowLists();
                chkLoginLevel("editNews");
                ShowInfo(this.Id);
            }
        }

        protected void BindDropdowLists()
        {
            var list = new NewTypeBLL().GetNewTypeList();
            ddlNewType.DataSource = list;
            ddlNewType.DataValueField = "NewTypeId";
            ddlNewType.DataTextField = "NewTypeName";
            ddlNewType.DataBind();
        }

        private void ShowInfo(int id)
        {
            var model = _bll.GetNewsInfo(id.ToString());
            txtNewTitle.Text = model.NewTitle;
            txtAuthor.Text = model.Author;
            txtSource.Text = model.Source;
            txtMetaKeywords.Text = model.MetaKeywords;
            txtMetaDescription.Text = model.MetaDescription;
            ddlNewType.SelectedValue = model.NewTypeId.ToString();
            FCKeditor.Value = model.NewContent;
            txtClick.Text = model.Click.ToString();
            if (model.IsTop == 1)
            {
                cblItem.Items[0].Selected = true;
            }
            if (model.IsRec == 1)
            {
                cblItem.Items[1].Selected = true;
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var model = new NewsInfo();
            model.NewId = Id;
            model.NewTitle = txtNewTitle.Text.Trim();
            model.Author = txtAuthor.Text.Trim();
            model.Source = txtSource.Text.Trim();
            model.MetaKeywords = txtMetaKeywords.Text.Trim();
            model.MetaDescription = txtMetaDescription.Text.Trim();
            model.NewTypeId = Convert.ToInt32(ddlNewType.SelectedValue);
            model.NewContent = FCKeditor.Value;
            model.Click = int.Parse(txtClick.Text.Trim());
            model.IsTop = 0;
            model.IsRec = 0;
            if (cblItem.Items[0].Selected == true)
            {
                model.IsTop = 1;
            }
            if (cblItem.Items[1].Selected == true)
            {
                model.IsRec = 1;
            }
            model.ModifyUser = "李波";
            model.ModifyTime = DateTime.Now;
            if (_bll.Update(model))
            {
                JscriptPrint("保存成功！", "NewsMasterForm.aspx", "Success");
            }
            else
            {
                JscriptPrint("保存失败！", "", "Error");
            }
        }
    }
}